/* ============================================================================= */
/*        TABLE B.2 - MATCHING PROCEDURE ROBUSTNESS CHECKS                      */
/* ============================================================================= */

clear all

/* Start logging */
capture log close
log using "$logs\regression_multiple_confs.log", replace

/* ============================================================================= */
/*        STEP 1: LOAD BASELINE DATA (UNMATCHED SAMPLE)                          */
/* ============================================================================= */

/* Load dataset with aggregated loan data */
use "$output\ifp_scrapped_aggregated_industries_modified.dta", clear

/* Keep only firms within [-12, 24] months relative to loan event */
keep if inrange(deltamonths, -12, 24)

/* Generate log-transformed loan variable */
gen lloan = log(stloan + ltloan + bm + bi + oc)

/* Merge with firm selection list for matching */
merge m:1 siren using "$output\ifp_ttest_siren_list.dta", keepusing(siren)
keep if _merge == 3  // Keep only matched firms
drop _merge

/* Regression without matching */
capture estimates drop *
qui eststo reg0: reghdfe lloan i1.p2p##i1.post, a(i.siren#i.year mdate) cluster(siren) 
estadd local yearfirm "Y", replace   
estadd local mdateFE "Y", replace  
estadd local firm "N", replace
estadd local IRL "N", replace

/* ============================================================================= */
/*        STEP 2: LOAD MATCHED SAMPLES & RUN REGRESSIONS                         */
/* ============================================================================= */

/* ===== Propensity Score Matching (PSM) - No Replacement ===== */
use "$output\matched_sample_ps_norep_industry_year.dta", clear

/* Keep relevant date range */
keep if inrange(mdate, mofd(date("20140101","YMD")), mofd(date("20191231","YMD")))
keep if inrange(deltamonths, -12, 24)

/* Generate log-transformed loan variable */
gen lloan = log(stloan + ltloan + bm + bi + oc)

/* Regression with PSM (No Replacement) */
qui eststo reg2: reghdfe lloan i1.p2p##i1.post [pweight=weight], a(i.siren#i.year mdate) cluster(siren) 
estadd local yearfirm "Y", replace   
estadd local mdateFE "Y", replace  
estadd local firm "N", replace
estadd local IRL "N", replace

/* ===== Propensity Score Matching (PSM) - With Replacement ===== */
use "$output\matched_sample_ps_rep_industry_year.dta", clear

/* Keep relevant date range */
keep if inrange(mdate, mofd(date("20140101","YMD")), mofd(date("20191231","YMD")))
keep if inrange(deltamonths, -12, 24)

/* Generate log-transformed loan variable */
gen lloan = log(stloan + ltloan + bm + bi + oc)

/* Regression with PSM (With Replacement) */
qui eststo reg3: reghdfe lloan i1.p2p##i1.post [pweight=weight], a(i.siren#i.year mdate) cluster(siren) 
estadd local yearfirm "Y", replace   
estadd local mdateFE "Y", replace  
estadd local firm "N", replace
estadd local IRL "N", replace

/* ===== 5-Nearest Neighbor Matching ===== */
use "$output\matched_sample_nn.dta", clear

/* Keep relevant date range */
keep if inrange(mdate, mofd(date("20140101","YMD")), mofd(date("20191231","YMD")))
keep if inrange(deltamonths, -12, 24)

/* Generate log-transformed loan variable */
gen lloan = log(stloan + ltloan + bm + bi + oc)

/* Regression with Nearest Neighbor Matching */
qui eststo reg4: reghdfe lloan i1.p2p##i1.post [pweight=weight], a(i.siren#i.year mdate) cluster(siren) 
estadd local yearfirm "Y", replace   
estadd local mdateFE "Y", replace  
estadd local firm "N", replace
estadd local IRL "N", replace 

/* ============================================================================= */
/*        STEP 3: ADDITIONAL ROBUSTNESS CHECKS                                   */
/* ============================================================================= */

/* ===== Firm Fixed Effects Regression (Alternative Specification) ===== */
qui eststo reg5: reghdfe lloan i1.p2p##i1.post [pweight=weight], a(i.siren mdate) cluster(siren) 
estadd local yearfirm "N", replace   
estadd local mdateFE "Y", replace  
estadd local firm "Y", replace
estadd local IRL "N", replace


/* ===== Excluding Outlying Platforms (Lendix & Lookandfin) ===== */
preserve
drop if outlyingplatform == 1
qui eststo reg7: reghdfe lloan i1.p2p##i1.post [pweight=weight], a(i.siren#i.year mdate) cluster(siren) 
estadd local yearfirm "Y", replace   
estadd local mdateFE "Y", replace  
estadd local firm "N", replace
estadd local IRL "N", replace
restore

/* ============================================================================= */
/*        STEP 4: EXPORT RESULTS TO LATEX TABLES                                */
/* ============================================================================= */

/* Export Table 4 col 1-2 */
esttab reg4 reg5 using "$tables/Table_4_fin_bank.tex", replace ///
	noomitted nobase fragment gap booktabs label nonote noobs nolines /// 
	b(%9.3f) drop(_cons) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) posthead(\midrule) ///
	mtitles("Unmatched" "5-nearest neighbor matching" "5-nearest neighbor matching" "PSM no rep." ///
	"PSM with rep." "Excl. Lendix & Lookandfin") ///
	order(1.p2p#1.post 1.post) varlabel(1.post "Post" 1.p2p#1.post `"FinTech $\times$ Post"' ) ///
	stats(yearfirm mdateFE firm IRL N r2, label(`"\midrule Firm-Year FE"' `"Month FE"' `"Firm FE"' `"IRL-Year FE"' `"\midrule N"' `"R-sq"') ///
	layout("\multicolumn{1}c{@}" "\multicolumn{1}c{@}" "\multicolumn{1}c{@}" "\multicolumn{1}c{@}") fmt(%9.0fc %9.0fc %9.0fc %9.0fc %9.0fc %9.2f ))

/* Export Table D1a */
esttab reg0 reg2 reg3 reg7 using "$tables/Table_D1_Matching_Procedure_fin_bank.tex", replace ///
	noomitted nobase fragment gap booktabs label nonote noobs nolines /// 
	b(%9.3f) drop(_cons) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) posthead(\midrule) ///
	mtitles("Unmatched" "5-nearest neighbor matching" "5-nearest neighbor matching" "PSM no rep." ///
	"PSM with rep." "Excl. Lendix & Lookandfin") ///
	order(1.p2p#1.post 1.post) varlabel(1.post "Post" 1.p2p#1.post `"FinTech $\times$ Post"' ) ///
	stats(yearfirm mdateFE firm IRL N r2, label(`"\midrule Firm-Year FE"' `"Month FE"' `"Firm FE"' `"IRL-Year FE"' `"\midrule N"' `"R-sq"') ///
	layout("\multicolumn{1}c{@}" "\multicolumn{1}c{@}" "\multicolumn{1}c{@}" "\multicolumn{1}c{@}") fmt(%9.0fc %9.0fc %9.0fc %9.0fc %9.0fc %9.2f ))

/* Close log file */
log close
